﻿using Rainbow.core;
using Rainbow.model;
using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace Rainbow.view
{
    /// <summary>
    /// ConnectDlg.xaml 的交互逻辑
    /// </summary>
    public partial class ConnectDlg : Window
    {
        public ConnectDlgViewModel windowViewModel;

        public ConnectDlg()
        {
            InitializeComponent();

            this.btnOK.Click += new RoutedEventHandler(OK_Click);
            this.btnClose.Click += new RoutedEventHandler(Close_Click);

            this.btnConnectManager.Click += new RoutedEventHandler(ConnectManager_Click);

            this.lbInfos.MouseDoubleClick += new MouseButtonEventHandler(MouseDouble_Click);

            windowViewModel = new ConnectDlgViewModel();
            this.DataContext = windowViewModel;
        }

        private void MouseDouble_Click(object sender, MouseButtonEventArgs e)
        {
            if (this.lbInfos.SelectedItem == null) return;

            ConnectInfoViewModel info = lbInfos.SelectedItem as ConnectInfoViewModel;
            if (info == null) return;

            windowViewModel.info = info.Clone();

            this.DialogResult = true;
        }

        private void ConnectManager_Click(object sender, RoutedEventArgs e)
        {
            ConnectManagerDlg dlg = new ConnectManagerDlg(windowViewModel.infos);
            dlg.Owner = this;
            dlg.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            bool? result = dlg.ShowDialog();
            if(result == true)
            {
                windowViewModel.info = dlg.resultInfo.Clone();
                this.DialogResult = true;
            }
            else
            {
                windowViewModel.infos.Clear();
                TW_App.connectInfos.ForEach(x =>
                {
                    windowViewModel.infos.Add(new ConnectInfoViewModel(x));
                    return false;
                });
            }
        }

        private void OK_Click(object sender, RoutedEventArgs e)
        {
            bool chkFlag = Check();
            if (!chkFlag) return;

            this.DialogResult = true;
        }

        private void Close_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private bool Check()
        {
            bool hasError = (Validation.GetHasError(tbHost)
                || Validation.GetHasError(tbPort)
                || Validation.GetHasError(tbUsername)
                || Validation.GetHasError(tbPassPhrase));

            return !hasError;
        }
    }

    public class ConnectDlgViewModel : ViewModelBase
    {
        public ConnectDlgViewModel()
        {
            info = new ConnectInfoViewModel();

            infos = new ObservableCollection<ConnectInfoViewModel>();
            TW_App.connectInfos.ForEach(x =>
            {
                infos.Add(new ConnectInfoViewModel(x));
                return false;
            });
        }

        private ConnectInfoViewModel m_info;
        public ConnectInfoViewModel info
        {
            get { return m_info; }
            set
            {
                m_info = value;
                NotifyPropertyChanged();
            }
        }

        private ObservableCollection<ConnectInfoViewModel> m_infos;
        public ObservableCollection<ConnectInfoViewModel> infos
        {
            get { return m_infos; }
            set
            {
                m_infos = value;
                NotifyPropertyChanged();
            }
        }
    }
}
